<!DOCTYPE html>

<html>
    <head>
        <title>Server-Sent Events Demo</title>
        <meta charset="UTF-8">
    </head>

    <body>

        <h1>Server-Sent Events Demo</h1>
        <p>This is a demo of Server-Sent Events (SSE) in HTML5.</p>
        <p id="output"></p>

        <script>
            if (typeof(EventSource) !== "undefined") {
                var sse = new EventSource('sse.php');
                //接收到消息
                sse.onmessage = function(event) {
                    var data = event.data;
                    console.log(data);
                    document.getElementById('output').innerHTML += data + '<br>';
                };
                //连接失败
                /* sse.onerror = function(event) {
                    document.getElementById('output').innerHTML = "EventSource failed.";
                }; */
            } else {
                alert("Your browser does not support server-sent events.");
            }
            
        </script>

    </body>
</html>

<!-- http://47.96.22.83:8187/thtml/html5/h5-sse.html -->
